package com.ruoyi.system.controller;

import java.util.Date;
import java.util.List;

import org.apache.commons.lang.StringUtils;
import org.apache.http.client.utils.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.system.domain.ProjectCreation;
import com.ruoyi.system.service.IProjectCreationService;
import com.ruoyi.system.service.ISysConfigService;

/**
 * 项目创建申请Controller
 * 
 * @author ruoyi
 * @date 2022-02-13
 */
@RestController
@RequestMapping("/labbase/project_creation")
public class ProjectCreationController extends BaseController {
	@Autowired
	private IProjectCreationService projectCreationService;
	@Autowired
    private ISysConfigService configService;

	/**
	 * 查询项目创建申请列表
	 */
	@PreAuthorize("@ss.hasPermi('labbase:project_creation:query')")
	@GetMapping("/list")
	public TableDataInfo list(ProjectCreation projectCreation) {
		startPage();
		List<ProjectCreation> list = projectCreationService.selectProjectCreationList(projectCreation);
		return getDataTable(list);
	}

	/**
	 * 导出项目创建申请列表
	 */
	// @PreAuthorize("@ss.hasPermi('labbase:project_creation:export')")
	/*
	 * @Log(title = "项目创建申请", businessType = BusinessType.EXPORT)
	 * 
	 * @GetMapping("/export") public AjaxResult export(ProjectCreation
	 * projectCreation) { List<ProjectCreation> list =
	 * projectCreationService.selectProjectCreationList(projectCreation);
	 * ExcelUtil<ProjectCreation> util = new
	 * ExcelUtil<ProjectCreation>(ProjectCreation.class); return
	 * util.exportExcel(list, "project_creation"); }
	 */

	/**
	 * 获取项目创建申请详细信息
	 */
	@PreAuthorize("@ss.hasPermi('labbase:project_creation:query')")
	@GetMapping(value = "/{instancetIds}")
	public AjaxResult getInfo(@PathVariable("instancetIds") Long instancetIds) {
		return AjaxResult.success(projectCreationService.selectProjectCreationById(instancetIds));
	}

	/**
	 * 新增项目创建申请
	 */
	@PreAuthorize("@ss.hasPermi('labbase:project_creation:edit')")
	@Log(title = "项目创建申请", businessType = BusinessType.INSERT)
	@PostMapping
	public AjaxResult add(@RequestBody ProjectCreation projectCreation) {
		projectCreationService.insertProjectCreation(projectCreation);
		projectCreation.setImages(projectCreationService.getInstanceFiles(projectCreation.getInstanceId()));
		return AjaxResult.success(projectCreation);
	}

	/**
	 * 修改项目创建申请
	 */
	@PreAuthorize("@ss.hasPermi('labbase:project_creation:edit')")
	@Log(title = "项目创建申请", businessType = BusinessType.UPDATE)
	@PutMapping
	public AjaxResult edit(@RequestBody ProjectCreation projectCreation) {
		projectCreationService.updateProjectCreation(projectCreation);
		projectCreation.setImages(projectCreationService.getInstanceFiles(projectCreation.getInstanceId()));
		return AjaxResult.success(projectCreation);
	}

	/**
	 * 删除项目创建申请
	 */
	@PreAuthorize("@ss.hasPermi('labbase:project_creation:edit')")
	@Log(title = "项目创建申请", businessType = BusinessType.DELETE)
	@DeleteMapping("/{instancetIds}")
	public AjaxResult remove(@PathVariable Long[] instancetIds) {
		return toAjax(projectCreationService.deleteProjectCreationByIds(instancetIds));
	}

	@PreAuthorize("@ss.hasPermi('labbase:project_creation:edit')")
	@Log(title = "提交项目创建流程", businessType = BusinessType.UPDATE)
	@PostMapping("/commit")
	public AjaxResult commit(@RequestBody ProjectCreation projectCreation) {
		String projectApplicationDate = configService.selectConfigByKey("projectApplicationDate");
		if(StringUtils.isNotEmpty(projectApplicationDate)){
			try{
			Date applicationDate = DateUtils.parseDate(projectApplicationDate,new String[]{"yyyy-MM-dd","yyyyMMdd","yyyy/mm/dd"});
			if(applicationDate.after(new Date())){
				return AjaxResult.error("项目申报开始日期:"+projectApplicationDate+", 时间未到不能提交.");
			}
			}catch(Exception e){
				return AjaxResult.error("项目申报开始日期的日期格式设置不正确,正确的格式比如：2022-01-01");
			}
		}
		return AjaxResult.success(projectCreationService.commitProjectCreation(projectCreation));
	}
}
